package com.yuhong.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuhong.reggie.dto.DishDto;
import com.yuhong.reggie.entity.Dish;
import com.yuhong.reggie.entity.DishFlavor;
import com.yuhong.reggie.entity.Page;
import com.yuhong.reggie.mapper.CategoryMapper;
import com.yuhong.reggie.mapper.DishFlavorMapper;
import com.yuhong.reggie.mapper.DishMapper;
import com.yuhong.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class DishServiceImpl implements DishService {
    @Autowired(required = false)
    private DishMapper dishMapper;
    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional  //springBoot默认开启事务注解,可直接使用
    @Override
    public boolean save(DishDto dishDto) {
        //1.完善菜品对象数据
        //(修改时间,添加时间,sort,是否禁售设置1为售)
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setSort(0);
        dishDto.setStatus(1);
        //2.调用数据访问层添加菜品,要求获取到添加后菜品主键id
        dishMapper.Insert(dishDto);

        //3.遍历口味记录列表完善数据
       List<DishFlavor> dishFlavorList = dishDto.getFlavors().stream().map((dishFlavor->{
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            return dishFlavor;
        })).collect(Collectors.toList());
        //4.调用数据访问层插入多条口味记录数据
        dishFlavorMapper.save(dishFlavorList);
        redisTemplate.delete("dish_"+dishDto.getCategoryId()+"_1"); 
        return true;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> findByPage(Integer page, Integer pageSize, String name) {
        //进行分页操作
        PageHelper.startPage(page,pageSize);
        //dishDtoList:志只是一页的数据,
        List<DishDto> dishDtoList = dishMapper.findAll(name);
        PageInfo<DishDto> dishPageInfo = new PageInfo<>(dishDtoList);

        Page<DishDto> objectPage = new Page<>();
        objectPage.setRecords(dishPageInfo.getList());
        objectPage.setTotal(dishPageInfo.getTotal());
        return objectPage;

    }

    /**
     * 回显
     *
     * @param id
     * @return
     */
    @Override
    public DishDto findById(Long id) {
        DishDto byId = dishMapper.findById(id);
        return byId;
    }

    /**
     * 根据类别id查询菜品列表
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        return dishMapper.findByCategoryId(categoryId);
    }

    /**
     * 根据类别和状态查询菜品列表list<dishDto>
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<DishDto> findByCategoryId(Long categoryId, Integer status) {
        //查询缓存类别的菜品列表,key="dish_"+categoryId+"_"+status
        String key ="dish_"+categoryId+"_"+status;
        List<DishDto> dishDtoList=(List<DishDto>) redisTemplate.opsForValue().get(key);
        if (dishDtoList==null) {
            List<Dish> dishList = dishMapper.findByCategoryIdAndStatus(categoryId, status);
            dishDtoList = dishList.stream().map((dish -> {
                DishDto dishDto = new DishDto();
                //只是把菜品复制到dto,口味没有
                BeanUtils.copyProperties(dish, dishDto);
                //根据菜品id获取口味列表数据封装到dishDto
                List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(dishDto.getId());
                dishDto.setFlavors(dishFlavorList);
                return dishDto;
            })).collect(Collectors.toList());
            //将数据库查到的数据写入缓存
            redisTemplate.opsForValue().set(key,dishDtoList);
        }
        return dishDtoList;
    }
}
